<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商家id" prop="businessId">
<!--        <el-input-->
<!--          v-model="queryParams.businessId"-->
<!--          placeholder="请输入商家id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.businessId" filterable placeholder="请选择">
          <el-option
            v-for="item in businessList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="群名称" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入群名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="群状态" prop="groupStatus">
        <el-select v-model="queryParams.groupStatus" placeholder="请选择群状态" clearable>
          <el-option
            v-for="dict in dict.type.chat_group_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="群介绍" prop="introduce">
        <el-input
          v-model="queryParams.introduce"
          placeholder="请输入群介绍"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户数" prop="userNum">
        <el-input
          v-model="queryParams.userNum"
          placeholder="请输入用户数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['chat:group:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['chat:group:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['chat:group:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['chat:group:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="群名称" align="center" prop="groupName" />
      <el-table-column label="商家id" align="center" prop="businessId">
        <template v-slot="slot">
          <el-button @click="openBusiness(slot.row)" type="primary" style="margin-left: 16px;">
            商家信息
          </el-button>
        </template>

<!--        需要通过级联通过商家id找到商家的数据（用户信息、昵称），然后通过按钮点击查看-->
      </el-table-column>
      <el-table-column label="群头像" align="center" prop="headImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.headImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="群状态" align="center" prop="groupStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.chat_group_status" :value="scope.row.groupStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="群介绍" align="center" prop="introduce" />
<!--      <el-table-column label="用户数" align="center" prop="userNum" />-->
<!--      <el-table-column label="查询编辑成员表" align="center"  />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['chat:group:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['chat:group:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改群对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="群名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入群名称" />
        </el-form-item>
        <el-form-item label="商家id" prop="businessId">
<!--          <el-input v-model="form.businessId" placeholder="请输入商家id" />-->
          <el-select v-model="form.businessId" filterable placeholder="请选择">
            <el-option
              v-for="item in businessList"
              :key="item.id"
              :label="item.nickName"
              :value="item.id"
              @change="changeUserId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="群状态" prop="groupStatus">
          <el-radio-group v-model="form.groupStatus">
            <el-radio
              v-for="dict in dict.type.chat_group_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="群介绍" prop="introduce">
          <el-input v-model="form.introduce" placeholder="请输入群介绍" />
        </el-form-item>
<!--        <el-form-item label="用户数" prop="userNum">-->
<!--          <el-input v-model="form.userNum" placeholder="请输入用户数" />-->
<!--        </el-form-item>-->
        <el-form-item label="群头像" prop="headImage">
          <image-upload v-model="form.headImage"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer
      title="商家信息"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose">
      <el-descriptions class="margin-top" style="margin: 10px" title="商家信息展示" :column="1"  border>
<!--        <template slot="extra">-->
<!--          <el-button type="primary" size="small">操作</el-button>-->
<!--        </template>-->
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            用户名
          </template>
          {{chatUser.nickName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-custom"></i>
            头像
          </template>
          <image-preview :src="chatUser.headImage" :width="50" :height="50"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-shopping-cart-2"></i>
            是否商家
          </template>
          {{chatUser.business==1?'是':'否'}}
        </el-descriptions-item>
<!--        <el-descriptions-item>-->
<!--          <template slot="label">-->
<!--            <i class="el-icon-tickets"></i>-->
<!--            备注-->
<!--          </template>-->
<!--          <el-tag size="small">{{chatUser.nickName}}</el-tag>-->
<!--        </el-descriptions-item>-->
<!--        <el-descriptions-item>-->
<!--          <template slot="label">-->
<!--            <i class="el-icon-office-building"></i>-->
<!--            联系地址-->
<!--          </template>-->
<!--          {{chatUser.nickName}}-->
<!--        </el-descriptions-item>-->
      </el-descriptions>

<!--      <el-descriptions class="margin-top" title="无边框列表" :column="3" :size="size">-->
<!--        <template slot="extra">-->
<!--          <el-button type="primary" size="small">操作</el-button>-->
<!--        </template>-->
<!--        <el-descriptions-item label="用户名">kooriookami</el-descriptions-item>-->
<!--        <el-descriptions-item label="手机号">18100000000</el-descriptions-item>-->
<!--        <el-descriptions-item label="居住地">苏州市</el-descriptions-item>-->
<!--        <el-descriptions-item label="备注">-->
<!--          <el-tag size="small">学校</el-tag>-->
<!--        </el-descriptions-item>-->
<!--        <el-descriptions-item label="联系地址">江苏省苏州市吴中区吴中大道 1188 号</el-descriptions-item>-->
<!--      </el-descriptions>-->

    </el-drawer>
  </div>
</template>

<script>
import { listGroup, getGroup, delGroup, addGroup, updateGroup,getBusiness } from "@/api/chat/group";
import {listUser} from "@/api/chat/user";

export default {
  name: "Group",
  dicts: ['chat_group_status'],
  data() {
    return {
      //查询所有是商家的用户
      businessList:[],
      queryBusinessList:{
        business:1
      },
      size:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 群表格数据
      groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        groupStatus: null,
        introduce: null,
        userNum: null,
        headImage: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //商家信息展示
      drawer: false,
      direction: 'rtl',
      chatUser:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询群列表 */
    getList() {
      this.loading = true;
      listGroup(this.queryParams).then(response => {
        this.groupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        groupId: null,
        businessId: null,
        groupStatus: null,
        introduce: null,
        userNum: null,
        createTime: null,
        updateTime: null,
        headImage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.groupId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加群";
      listUser(this.queryBusinessList).then(response => {
        this.businessList = response.rows;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const groupId = row.groupId || this.ids
      getGroup(groupId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改群";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.groupId != null) {
            updateGroup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const groupIds = row.groupId || this.ids;
      this.$modal.confirm('是否确认删除群编号为"' + groupIds + '"的数据项？').then(function() {
        return delGroup(groupIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('chat/group/export', {
        ...this.queryParams
      }, `group_${new Date().getTime()}.xlsx`)
    },
    /**
     * 商家弹窗控制*/
    handleClose(done) {
      done();
      // this.$confirm('确认关闭？')
      //   .then(_ => {
      //     done();
      //   })
      //   .catch(_ => {});
    },
    openBusiness(business){
      // const groupId = row.groupId || this.ids
      // getBusiness(businessId).then(response => {
      //   let business = response.data;
      //   console.info(business)
      // });
      // getBusiness();
      // alert(businessId)

      this.chatUser=business.chatUser
      console.info(this.chatUser)
      this.drawer = true
    },
    changeUserId(userId){
      this.form.businessId=userId
    }
  }
};
</script>
